// 元素.onXXXX = function(){}
// window.onload = function(){}
// xxx.addEventListener('click', function(){})

// document.querySelector();
// 对象取值、设置值、函数调用  内置对象

// 定义类
class Person {
    // 构造函数
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    // 方法
    say() {
        console.log(this.name + "说：你好");
    }
}

// // 实例化对象
// let p = new Person("张三", 18);
// // 赋值
// p.name = "张三";
// p.age = 19;
// console.log(p);

// p.say();


// 定义类(定义构造函数)
function Student(name) {
    this.name = name;

    // 方法
    this.say = function () {
        console.log(this.name + "说：你好");
    }
}

// 实例化对象
let stu = new Student("李四");
stu.say();

// 定义对象
let obj = {
    name: "",
    age: 0,
    say: function () {
        console.log(this.name + "说：你好");
    }
};

obj.name = "王五";

obj.say();

// 定义一个 Car 类，包含 name、price、color 属性，包含 run 方法
class Car {
    constructor(name, price, color) {
        this.name = name;
        this.price = price;
        this.color = color;
    }

    run() {
        console.log(this.name + "在跑...");
    }
}

// 定义一个 BMW 类，继承 Car 类，包含 GPS 属性，重写 run 方法
class BMW extends Car {

    constructor(name, price, color, gps) {
        super(name, price, color);
        this.gps = gps;
    }

    run() {
        console.log(this.name + "在跑，GPS：" + this.gps);
    }
}

// 实例化对象
let car = new Car("奔驰", 1000000, "黑色");
car.run();

let bmw = new BMW("宝马", 1000000, "黑色", "北斗");
bmw.run();


// 通过原型链实现继承
function Animal(name) {
    this.name = name;
}

Animal.prototype.say = function () {
    console.log(this.name + "说：hello");
}

function Dog(name) {
    this.name = name;
}

// 继承
Dog.prototype = new Animal();

let dog = new Dog("旺财");
dog.say();

// 查看对象的原型
console.log(dog.__proto__);

// 查看对象的原型
console.log(Dog.prototype);

// Object 是所有对象的父类
Object.prototype.say = function () {
    console.log("hello");
}

class A {

}

new A().say();

Array.prototype.toString = function () {
    // 数组转字符串
    let str = "";
    for (let i = 0; i < this.length; i++) {
        str += this[i] + ",";
    }
    return str.substring(0, str.length - 1) + "--kfm";
}

Array.prototype.add = function (num) {
    this.push(num);
}

let arr = [1, 2, 3, 4];

console.log(arr.toString());

console.log(Array.prototype);